from django.db import transaction, connection


def get_user_role(user):
	cursor = connection.cursor()
	cursor.execute(
		"""
		SELECT name 
		FROM auth_group 
		WHERE id = (SELECT group_id 
					FROM auth_user_groups
					WHERE user_id = %s);
		""", [user.id])
	row = cursor.fetchone()
	if row != None:
		return row[0]
	else:
		return 'anonymous'

def role_match(user, role):
	user_role = get_user_role(user)
	if user_role == role:
		return True
	else:
		return False

def fetch_all_departments():
	cursor = connection.cursor()
	cursor.execute("""SELECT * FROM department;""")
	rows = cursor.fetchall()
	return rows

def fetch_all_medicines():
	cursor = connection.cursor()
	cursor.execute("""SELECT * FROM medicine_info;""")
	rows = cursor.fetchall()
	return rows